package com.info.provider.service.impl;

import com.info.provider.entity.Think;
import com.info.provider.repository.ThinkRepository;
import com.info.provider.service.ThinkService;
import com.mars.app.exception.ErrorCodeMsg;
import com.mars.app.exception.SystemException;
import com.mars.app.utils.UUIDUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by pengshuo on 2018/2/2 12:11
 * Description:
 * Modified By:
 * Version:
 *
 * @author pengshuo
 */
@Service
public class ThinkServiceImpl implements ThinkService {

    private Logger logger = LogManager.getLogger(this.getClass());

    @Autowired
    private ThinkRepository thinkRepository;

    /**
     * create think
     *
     * @param think think
     * @return think
     * @throws SystemException
     */
    @Override
    public Think create(Think think) throws SystemException {
        try {
            // uuid
            think.setUuid(UUIDUtil.think());
            // create date
            think.setCreateDate(new Date());
            return thinkRepository.insert(think);
        }catch (Exception e){
            logger.error("新增 Think 异常：{}",e);
            throw new SystemException(ErrorCodeMsg.DAO_OPERATE_ERROR,e);
        }
    }

    /**
     * get think by primary key
     *
     * @param uuid primary key
     * @return Think
     * @throws SystemException
     */
    @Override
    public Think get(String uuid) throws SystemException {
        try {
            return thinkRepository.findOne(uuid);
        }catch (Exception e){
            logger.error("get Think 异常：{}",e);
            throw new SystemException(ErrorCodeMsg.DAO_OPERATE_ERROR,e);
        }
    }
}
